package com.immoc.jdbc.hrapp.command;

import com.immoc.jdbc.common.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class DeleteCommand implements Command {
    @Override
    public void execute() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入员工编号");
        int eno = scanner.nextInt();

        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection =  DbUtils.getConnection();
            String sql = "delete from employee  where eno=?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, eno);
            int cnt = pstmt.executeUpdate();
            if(cnt==0){
                System.out.println("cnt:"+ cnt);
                System.out.println(eno+"失败");
            }else{
                System.out.println("cnt:"+ cnt);
                System.out.println(eno+"成功");
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.closeConnection(connection);
        }
    }
}
